package com.guodu.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guodu.dao.NoticemanagerDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.Filenotice;
import com.guodu.entity.Noticemanager;
import com.guodu.entity.NoticemanagerUnits;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.NoticemanagerService;
import com.guodu.vo.NoticemanagerQueryVo;
import com.guodu.vo.NoticemanagerVo;

@Service("noticemanagerService")
public class NoticemanagerServiceImpl implements NoticemanagerService {
	
	
	@Resource
	private NoticemanagerDao noticemanagerDao;

	@Resource
	private UnitsDao unitsDao;
	
	@Resource
	private UserInfoDao userInfoDao;
	

	@Override
	public long findSizeByVo(NoticemanagerQueryVo nqv, Units units,String fUnitName) {
		// TODO Auto-generated method stub
		return noticemanagerDao.findSizeByVo(nqv, units,fUnitName);
	}

	@Override
	public List<NoticemanagerVo> findByVo(NoticemanagerQueryVo nqv,
			Units units, int curPage, int pageSize,String fUnitName) {
		// TODO Auto-generated method stub
		List<Noticemanager> list = noticemanagerDao.findByVo(nqv, units, curPage, pageSize,fUnitName);
		List<NoticemanagerVo> noticemanagerVos = new ArrayList<NoticemanagerVo>();
		for(Noticemanager noticemanager : list ){
			NoticemanagerVo noticemanagerVo = new NoticemanagerVo(noticemanager);
			UserInfo userInfo = userInfoDao.findById(noticemanager.getCreatePeople());
			List<Filenotice> filenotice = noticemanagerDao.findFile(noticemanager.getNoticeFile());
			if (filenotice.size()>0&&filenotice!=null) {
				for (Filenotice fileTechnical2 : filenotice) {
					noticemanagerVo.setFileTechnicalName(fileTechnical2.getFilenoticeName());
					noticemanagerVo.setFileName(fileTechnical2.getFileName());
					noticemanagerVo.setFileUrl(fileTechnical2.getFileRealname());
				}
			}
			if (noticemanager.getUnitsId()!=null) {
				Units unitsInfo = unitsDao.findById(noticemanager.getUnitsId());
				noticemanagerVo.setUnitsName(unitsInfo.getUnitsName());
				noticemanagerVo.setIndustry(unitsInfo.getIndustry());
				
				if (noticemanager.getCreatePeople()==1) {
					if (noticemanager.getUnitsId()==null&&"".equals(noticemanager.getUnitsId())) {
						Long id = null;
						noticemanagerVo.setUnitsLevel(id);
					}
					if (noticemanager.getUnitsId()==null&&noticemanager.getUnitsId()!=null) {
						if (unitsInfo.getFunitsId()!=null) {
							noticemanagerVo.setUnitsLevel((long)2);
						}else {
							noticemanagerVo.setUnitsLevel((long)1);
						}
					}
				}else{
					if (unitsInfo.getFunitsId()!=null) {
						noticemanagerVo.setUnitsLevel((long)2);
					}else {
						noticemanagerVo.setUnitsLevel((long)1);
					}
				}
				if (userInfo.getUnitsId()!=null) {
					Units createUnits =  unitsDao.findById(userInfo.getUnitsId());
					noticemanagerVo.setUnitsLevel(createUnits.getUnitsLevel());
				}
				
				if(unitsInfo.getFunitsId()!=null){
					Units funitsInfo = unitsDao.findById(unitsInfo.getFunitsId());
					noticemanagerVo.setFunitsName(funitsInfo.getUnitsName());
				}
			}else{
				Long id = null;
				noticemanagerVo.setUnitsLevel(id);
			}
			noticemanagerVo.setUserName(userInfo.getUserName());
			noticemanagerVos.add(noticemanagerVo);
		}
		return noticemanagerVos;
	}

	@Override
	public String save(Noticemanager noticemanager) {
		// TODO Auto-generated method stub
		return noticemanagerDao.save(noticemanager);
	}

	@Override
	public Noticemanager findById(String purchaseId,String flag,Units units) {
		// TODO Auto-generated method stub
		if(flag!=null&&flag.equals("0")){
			noticemanagerDao.updateNotice(units, purchaseId);
		}
		return noticemanagerDao.findById(purchaseId);
	}

	@Override
	public void saveFile(Filenotice filenotice) {
		// TODO Auto-generated method stub
		noticemanagerDao.saveFile(filenotice);
	}

	@Override
	public void delByIds(String[] delIds) {
		// TODO Auto-generated method stub
		for (int i = 0; i < delIds.length; i++) {
			Noticemanager noticemanager = noticemanagerDao.findById(delIds[i]);
			noticemanagerDao.delZi(noticemanager.getNoticeFile());
		}
		noticemanagerDao.delByIds(delIds);
	}

	@Override
	public List<Filenotice> findFile(String noticeFile) {
		// TODO Auto-generated method stub
		return noticemanagerDao.findFile(noticeFile);
	}

	@Override
	public Noticemanager findByUnits(String unitsId) {
		// TODO Auto-generated method stub
		return noticemanagerDao.findByUnits(unitsId);
	}

	@Override
	public void saveNoticeUnits(List<NoticemanagerUnits> nus) {
		// TODO Auto-generated method stub
		noticemanagerDao.saveNoticeUnits(nus);
	}

}
